package com.hnluchuan.staff.web.controller.admin;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.hnluchuan.staff.dto.EmployeeDTO;
import com.hnluchuan.staff.dto.UserDTO;
import com.hnluchuan.utils.exception.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hnluchuan.staff.dto.NewsDTO;
import com.hnluchuan.staff.service.NewsService;

import com.hnluchuan.core.support.Page;
import com.hnluchuan.staff.web.controller.BaseController;

@Controller
@RequestMapping(value = "/admin/news", produces="text/html;charset=UTF-8")
public class AdminNewsController extends BaseController {

	@Autowired
	private NewsService newsService;

	@RequestMapping(value = "/detail")
	public String detail(Map<String, Object> map, Long id){
		UserDTO userDTO=getCurrentUserDTO();
		System.out.print(userDTO.getId());
		if (id != null) {
			NewsDTO dto = newsService.load(id);
			map.put("n", dto);
			if(userDTO.getType()!=1)
			{
				return "admin/news/news_employee";
			}
		}
		if(userDTO.getType()!=1)
		{
			map.put("info","你没有权限!!");
			return "admin/employee/error";
		}
		return "admin/news/news_detail";
	}

	@ResponseBody
	@RequestMapping(value = "/save")
	public String save(NewsDTO dto) throws Exception {
		UserDTO userDTO=getCurrentUserDTO();
		if (dto.getId() == null) {
			dto.setCreator(getCurrentUserDTO());
			SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
			Date datetime=simpleDateFormat.parse(simpleDateFormat.format(Calendar.getInstance().getTime())); //获取系统时间
			dto.setCreateTime(datetime);
			dto.setUpdator(getCurrentUserDTO());
			newsService.create(dto);
		} else if (userDTO.getType()==1){
//			throw new RuntimeException("请实现更新逻辑，并删除该行");
			NewsDTO old = newsService.load(dto.getId());
			// 确定哪些字段是在详情页面需要修改的，不在详情页修改的，就不需要在这里set值。updated和updatedBy不需要在这里处理，框架会统一处理
			old.setId(dto.getId());
//			old.setName(dto.getName());
			old.setSerialNum(dto.getSerialNum());
			old.setStatus(dto.getStatus());
			old.setTitle(dto.getTitle());
			old.setSummary(dto.getSummary());
			old.setThumb(dto.getThumb());
			old.setContent(dto.getContent());
			old.setCreator(dto.getCreator());
			old.setCreateTime(dto.getCreateTime());
			old.setUpdator(dto.getUpdator());
			old.setUpdateTime(dto.getUpdateTime());
			newsService.updateAllFields(old);
		}else {
			throw new RuntimeException("您没有修改权限");
		}

		return ok();
	}

	@ResponseBody
	@RequestMapping(value = "/delete")
	public String delete(Long[] ids,Map<String,Object> map,Page page,NewsDTO dto) throws IOException {
		UserDTO userDTO=getCurrentUserDTO();
		if (userDTO.getType()==1){
			newsService.deleteByIds(ids);
		}else {
			throw new BizException("您没有权限删除");
		}
		List<NewsDTO> dtos = newsService.find(dto,page);
		map.put("list", dtos);
		return ok();
	}

	@RequestMapping(value = "/list")
	public String list(NewsDTO dto, Boolean search, Map<String, Object> map, Page page) {
		UserDTO userDTO=getCurrentUserDTO();
		List<NewsDTO> dtos = newsService.find(dto, page);
		if(userDTO.getType()!=1)
		{
			if(dtos!=null&&dtos.size()>0)
			{
				for (int i=0;i<dtos.size();i++
						) {
					NewsDTO n=dtos.get(i);
					if(n.getStatus()!=2)
					{
						dtos.remove(i);
						i--;
					}
				}
			}
		}
		map.put("type",userDTO.getType());
		map.put("list", dtos);
		map.put("query", dto);
		map.put("search", search);
		return "admin/news/news_list";
	}

	@RequestMapping(value = "/search")
	public String search(NewsDTO dto, Map<String, Object> map, Page page) {
		List<NewsDTO> dtos = newsService.find(dto, page);
		map.put("list", dtos);
		map.put("query", dto);
		return "admin/news/news_search";
	}

}
